<?php
namespace App\Http\Controllers\Admin\Auth;

use App\Services\Admin\ActionService;
use App\Utilities\JiugeForm;
use Illuminate\Http\Request;

class ActionController extends BaseController
{
    /**
     * 后台功能
     */

    protected $view = 'admin.auth.action.';//视图前缀
    protected $url = 'action';//路由前缀
    protected $model;
    protected $moduleId = 2;
    protected $formElementArr = [//表单元素
        //array(表单选项,中文名称,name字段名称,表单提示,js验证规则)
        array(1,'操作名称','name','操作名称，2-20字符，例：操作列表',1),
        array(1,'路由','url','路由，例：/dzadmin/action',11),
        array(5,'父级','pid','父级',6),
    ];
    protected $fieldShowArr = [
        'id' => 'ID',
        'name' => '操作名称',
        'url' => '路由',
        'parentName' => '父级',
        'createTime' => '创建时间',
    ];

    public function __construct()
    {
        parent::__construct();
        $this->prefix_url = $this->prefix_url.'/'.$this->url;
        $this->model = new ActionService();
        view()->share('prefix_url',$this->prefix_url);
        view()->share('jiugeForm',new JiugeForm($this->prefix_url));
    }

    public function index(Request $request)
    {
        $moduleId = isset($request->module) ? $request->module : $this->moduleId;
        $datas = $this->model->getIndex($moduleId);
//        if (!count($datas)) { dd('待开发！'); }
        return view($this->view.'index',array(
            'datas' => $datas,
            'prefix_url' => $this->prefix_url,
        ));
    }

    public function create(Request $request)
    {
        $moduleId = isset($request->module) ? $request->module : $this->moduleId;
        return view($this->formView.'edit',array(
            'sels' => $this->formElementArr,
            'options' => array(
                'pid' => $this->model->getForms(),
            ),
        ));
    }

    public function store(Request $request)
    {
        $moduleId = isset($request->module) ? $request->module : $this->moduleId;
        $data = $this->getData($request);
        $this->model->add($data);
        return redirect($this->prefix_url);
    }

    public function edit(Request $request,$id)
    {
        $moduleId = isset($request->module) ? $request->module : $this->moduleId;
        return view($this->formView.'edit',array(
            'sels' => $this->formElementArr,
            'options' => array(
                'pid' => $this->model->getForms(),
            ),
            'data' => $this->getModel($id),
        ));
    }

    public function update(Request $request,$id)
    {
        $moduleId = isset($request->module) ? $request->module : $this->moduleId;
        $model = $this->getModel($id);
        $data = $this->getData($request);
        $this->model->modify($data,$id);
        return redirect($this->prefix_url);
    }

    public function getData(Request $request)
    {
        $name = $request->name;
        $url = $request->url;
        $pid = $request->pid;
        if (!$name || !$url) {
            echo "<script>alert('数据不全！');history.go(-1);</script>"; exit;
        }
        return array(
            'name' => $name,
            'url' => $url,
            'pid' => $pid,
        );
    }

    public function getModel($id)
    {
        $model = $this->model->getOne($id);
        if (!$model) {
            echo "<script>alert('记录不存在！');history.go(-1);</script>"; exit;
        }
        return $model;
    }

    /**
     * 设置删除
     */
    public function setDel(Request $request)
    {
        $id = $request->id;
        $oper = $request->oper;  //1删除，2还原
        $model = $this->getModel($id);
        if ($oper==1) {
            if ($model->del) {
                echo "<script>";
                echo "alert('记录已删除！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $this->model->setDel($id);
        } else {
            if (!$model->del) {
                echo "<script>";
                echo "alert('记录已还原！');";
                echo "history.go(-1);";
                echo "</script>";
                exit;
            }
            $this->model->setDelRedo($id);
        }
        return redirect($this->prefix_url);
    }
}